//  this library.
import 'package:flutter/material.dart';
import 'package:flutter_animate/flutter_animate.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:hktk_app/painter/six_pointed_star_painter.dart';
import 'package:hktk_app/util/font_util.dart';
import 'package:hktk_app/util/view_util.dart';

/// 显示标签组件，如：购买页面添加科目和已选科目的科目显示
class ShowLabelWidget extends StatelessWidget {
  /// 显示内容
  final String text;

  /// 在列表中的下标
  final int? index;

  /// 字体样式
  final TextStyle? textStyle;

  /// 背景色
  final Color? backgroundColor;

  /// 内边距
  final EdgeInsets? textPadding;

  /// 圆角
  final double? radius;

  /// 是否显示图标
  final bool showIcon;

  /// 图标默认样式，true 为勾，false 为叉
  final bool? iconStyle;

  /// 点击方法
  final ValueChanged<int>? iconFun;

  /// 显示已购标签
  final bool showBuy;

  final bool showFlag;

  const ShowLabelWidget({
    super.key,
    required this.text,
    this.index,
    this.textStyle,
    this.backgroundColor,
    this.textPadding,
    this.radius,
    this.showIcon = false,
    this.iconStyle = true,
    this.iconFun,
    this.showBuy = false,
    this.showFlag = true,
  });

  /// 取消标签，蓝色带叉
  ShowLabelWidget.cancel({
    super.key,
    required this.text,
    required this.index,
    required this.iconFun,
    this.showIcon = true,
    this.textPadding,
    this.radius,
    this.showBuy = false,
    this.showFlag = true,
  })  : iconStyle = false,
        backgroundColor = colorBlue2,
        textStyle = FontUtil.fontBlue1(16);

  /// 被禁止标签
  ShowLabelWidget.disabled({
    super.key,
    required this.text,
    this.textPadding,
    this.radius,
    this.showBuy = false,
    this.showFlag = true,
  })  : index = 0,
        textStyle = FontUtil.fontGrey6(16),
        backgroundColor = colorWhite2,
        showIcon = false,
        iconStyle = true,
        iconFun = null;

  /// 订单支付，白色带勾
  ShowLabelWidget.order({
    super.key,
    required this.text,
    this.radius,
    this.showBuy = false,
    this.showFlag = true,
  })  : index = 0,
        textPadding = null,
        textStyle = FontUtil.fontBlack1(16),
        backgroundColor = colorWhite1,
        showIcon = true,
        iconStyle = true,
        iconFun = null;

  @override
  Widget build(BuildContext context) {
    return Container(
      decoration: BoxDecoration(
        color: backgroundColor ?? colorWhite2,
        borderRadius: BorderRadius.all(Radius.circular(radius ?? 18)),
      ),
      child: Row(
        mainAxisSize: MainAxisSize.min,
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Container(
            padding: textPadding ??
                EdgeInsets.only(
                  left: 10,
                  top: 5,
                  bottom: 5,
                  right: 10,
                ),
            child: Text(
              text,
              style: textStyle ?? FontUtil.fontBlack1(16),
            ),
          ),
          _labelItem(),
        ],
      ),
    )
        .animate().fadeIn()
    ;
  }

  /// 显示
  _labelItem() {
    return Column(
      mainAxisSize: MainAxisSize.min,
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      crossAxisAlignment: CrossAxisAlignment.end,
      children: [
        _iconShow(),
        if (showBuy && showFlag) _buyLabelItem(),
      ],
    );
  }

  /// 显示图标
  _iconShow() {
    if (!showIcon) {
      return SizedBox(
        width: 0,
        height: 0,
      );
    } else {
      return GestureDetector(
        onTap: () {
          if (iconFun != null) {
            iconFun!(index!);
          }
        },
        child: Container(
          padding: EdgeInsets.fromLTRB(5, 5, 5, 5),
          child: SvgPicture.asset(
            iconStyle!
                ? "images/package/show_label_1.svg"
                : "images/package/show_label_2.svg",
            width: 12,
            height: 12,
          ),
        ),
      );
    }
  }

  /// 已购买标签
  _buyLabelItem() {
    return SizedBox(
      width: 32,
      height: 32,
      child: CustomPaint(
        // size: Size(100, 100),
        painter: SixPointedStarPainter(color: colorBlue26),
        child: Center(
            child: Text(
          "购",
          style: TextStyle(color: Colors.white),
        )),
      ),
    );
  }
}
